$(function(){
	
	/*=== 全屏滚动插件初始化 ===*/
    $('#J-g-ct').fullpage({
        // 每一屏背景色
        sectionsColor: ['#0d7fe0', '#3db275', '#ea7231', '#f5a527', '#f55858', '#fafafa', '#348af5'],
		css3: true,
		navigation: true,
		navigationPosition: "right",
		anchors: ["P1", "P2", "P3", "P4", "P5", "P6", "P7"],
		afterRender: function() {
			// 右侧翻页菜单
			$("#fp-nav em").hide(), $("#fp-nav").find("a.active").html("<span></span><em>01</em>").find("em").show()
		},
		onLeave: function(index, next) {
			// 显示默认隐藏的页面
			$('#J_p' + next + ' .m-page' + next).show();
			if(next == 2){
				$('#J_p2 .m-page2-ft').show();
			}
			if(next == 3 || next == 4){
				$('#J_p' + next + ' .m-page' + next + '-bg').show();
			}
			// 右侧翻页菜单
			$("#fp-nav em").hide(), $("#fp-nav").find("a").eq(next - 1).html("<span></span><em>0" + next + "</em>").find("em").show(), $(".section" + index).removeClass("animate"), index == 3 && $(".pos").hide()
		},
		afterLoad: function(anchorLink, index){
			if(index == 2){
				$('#J_p3 .m-page3-ft').show();
				/*屏2动画*/
				$('#J_p2_about').delay(300).animate({
					right: '0px',opacity: '1'
				}, 600, 'easeInQuad');
			}
			if(index == 3){
				/*屏3动画*/
				$('#J_p3_about').delay(300).animate({
					left: '0px',opacity: '1'
				}, 600, 'easeInQuad');
			}
		}
    });

	/*=== 1屏，在线申请 ===*/
	$('#J_p1_gop7').click(function(){
		$('#fp-nav a').last().click();
	});

    /*=== 合作伙伴，滚动插件 ===*/
    $appLise = $('#J-appList');
    $appLise.scrollBox({
	  parentWidth: 880,
	  width: 176,
	  prevSelector: '#J-next',
	  nextSelector: '#J-prev',
	  scrollWidth: 176
	});
	
	// 设置验证码
	$li = $('#J-vcode');
	$li.find('img').attr('src', createVcode());
	$li.find('a').click(function(){
		$li.find('img').attr('src', createVcode());
	});
	
	/*=== 表单验证 ===*/
	var _form = $('#J-fm'),
		tpl_tips = '<span id="J-tips" class="u-tips"></span>',
		_qq = _form.find('input[name ="qq"]'),
		_phone = _form.find('input[name ="phone"]'),
		_vcode = _form.find('input[name ="valiCode"]'),
		phoneRule = /^((\d{11})|(\d{7,8})|(\d{4}|\d{3})-(\d{7,8}))$/,
		qqRule = /^\d{6,12}$/,
		vcodeRule = /^[A-Za-z]{4,8}$/,
		$liBtn = $("#J-btn"),
		$tips = $("#J-tips"),
		qqFlag = false,
		phoneFlag = false,
		vcodeFlag = false;
			
	_phone.blur(function(){
		if(!phoneRule.test($(this).val())){
			if(!qqFlag){
				showTips("请输入正确格式电话号码，如：13800000000,1234-12345678");
			}
			phoneFlag = false;
		}else{
			removeTips();
			phoneFlag = true;
			checkExistVcode();
		}
		//alert("phone"+phoneFlag);
		checkSubmit();
	});
	
	_qq.blur(function(){
		if(!qqRule.test($(this).val())){
			if(!phoneFlag){
				showTips("请输入6-12位QQ号码");
			}
			qqFlag = false;
		}else{
			removeTips();
			qqFlag = true;
			checkExistVcode();
		}
		//alert("qq"+qqFlag);
		checkSubmit();
	});
	
	_vcode.blur(function(){
		if(!vcodeRule.test($(this).val())){
			showTips("请输入4-8位验证码");
			vcodeFlag = false;
		}else{
			removeTips();
			vcodeFlag = true;
		}
		//alert("code"+vcodeFlag);
		checkSubmit();
	});
	    
	// 提交表单
	_form.submit(function(){
		_qq.blur();
		_phone.blur();
		_vcode.blur();
		return checkSubmit();
	})
	
	// 显示提示
	function showTips(msg){
		removeTips();
		$tips.html(msg);
		$tips.show();
	}
	
	// 删除提示
	function removeTips(){
		if($tips){
			$tips.hide();
		};
	}
	
	// 检查验已有的证码
	function checkExistVcode(){
		if(_vcode.val()){
			_vcode.blur();
		}
	}
	
	// 检查是否可提交
	function checkSubmit(){
		if( (phoneFlag && vcodeFlag) || qqFlag && vcodeFlag ){
			$liBtn.find("input").removeAttr("disabled");
			$liBtn.addClass("m-btn z-true");
			return true;
		}else{
			$("#J-btn input").attr("disabled", "disabled");
			$liBtn.removeClass("z-true");
		}
		return false;
	}
    
});
	
// 生成验证码
function createVcode(){
    return $('#J-vcode img').data('src') + '?' + Math.floor( Math.random()*10e8 );
}
